import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class Client 
{
	static Window windows;
	static Socket socket;
	static BufferedReader reader = null;
	static PrintWriter writer = null;
	
	public static void main(String[] args)
	{
		String name = args[0];
		String adress = args[1];
		int port = 0;
		try
		{
			port = Integer.parseInt(args[2]);
			socket = new Socket(adress, port);
     	  		writer = new PrintWriter(socket.getOutputStream(), true);
      			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		}
		catch (NumberFormatException e)
		{
			System.out.println("Bad number");
			System.exit(1);
		}
		catch (UnknownHostException e) 
		{
      	 	System.out.println("Unknown host: localhost"); 
			System.exit(1);
     	}
     	catch (IOException e) 
		{
			System.out.println("No I/O"); 
			System.exit(1);
     	} 
		
		windows = new Window();
		windows.addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{
				try
				{
					socket.close();
					reader.close();
					writer.close();
				}
				catch(IOException e1)
				{}
   				System.exit(0);
			}
        });
		windows.getNameLab().setText(name);
		windows.getPortLab().setText(args[2]);
		windows.pack();	
		String line = "";
		String[] temp = line.split("-");
		while(!temp[0].equals("MONEY"))
		{
			try
			{
				writer.println("NAME-"+ name);
				line = reader.readLine();
				temp = line.split("-");
			}
			catch(IOException e)
			{}
		}
		windows.getLabel(6).setText(temp[1]);
		line = "EMPTY EMPTY";
	
		

		initialize();
		hide();		
		waitFor();
	}
	
	public static void  waitFor()
	{
		hide();
		
		String line = "";
		
		while(!(line.equals("YES C") || line.equals("YES A")))
		{
			try
			{
				writer.println("CAN I START");
				line = reader.readLine();				
			}
			catch(IOException e)
			{}
			if(line.substring(0, 3).equals("POT"))
			{
				String[] help = line.split(" ");
				windows.getLabel(10).setText(help[1]);
			}
			else if(line.substring(0, 5).equals("CARDS"))
			{
				String[] temp = line.split(" ");
				int j = 1;
				for(int i = 11; i < 15; i++)
				{
					windows.getLabel(i).setText(temp[j]);
					j++;
				}
			}
			else if(line.length() > 8 && line.substring(0, 8).equals("BIGBLIND"))
			{
				String[] help = line.split(" ");
				windows.getLabel(6).setText(help[2]);
				windows.getLabel(8).setText(help[3]);
				windows.getArea().append(help[0] + "\n");
			}
			else if(line.length() > 10 && line.substring(0, 10).equals("SMALLBLIND"))
			{
				String[] help = line.split(" ");
				windows.getLabel(6).setText(help[2]);
				windows.getLabel(8).setText(help[3]);
				windows.getArea().append(help[0] + "\n");
			}
			else if(!line.equals("") && !line.substring(0, 3).equals("YES"))
				windows.getArea().append(line + "\n");
		}

		if(line.equals("YES C"))
			showChanging();
		else if(line.equals("YES A"))
			showAuction();
	}
	
	public static void hide()
	{
		for(int i = 0; i < 6; i++)
			windows.getButton(i).setVisible(false);
		windows.getChange().setVisible(false);
	}
	
	public static void showAuction()
	{
		for(int i = 0; i < 6; i++)
			windows.getButton(i).setVisible(true);
	}
	
	public static void showChanging()
	{
		windows.getChange().setVisible(true);
	}
	
	public static void initialize()
	{
		windows.getButton(0).addActionListener(check);
		windows.getButton(1).addActionListener(bet);
		windows.getButton(2).addActionListener(raise);
		windows.getButton(3).addActionListener(call);
		windows.getButton(4).addActionListener(fold);
		windows.getButton(5).addActionListener(allin);
		windows.getChange().addActionListener(change);
	}
	
	static ActionListener change = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			JCheckBox[] temp = windows.getChoose();
			String toChange = "";
			
			for(int i = 0; i < temp.length; i++)
				if(temp[i].isSelected())
					toChange += " 1" ;
				else toChange += " 0";
			
			writer.println("CHANGE" + toChange);
			
			try
			{
				windows.getArea().append(reader.readLine() + "\n");
			}
			catch(IOException e)
			{}
			
			for(int i = 0; i < temp.length; i++)
				temp[i].setSelected(false);
			hide();
			waitFor();
		}
	};
	
	static ActionListener check = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			writer.println("CHECK");
			String line = "";
			do
			{
				try
				{
					line = reader.readLine();
				}
				catch(IOException e)
				{
				}
			}
			while(!line.equals("OK") && !line.equals("ERROR"));
			if(line.equals("ERROR"))
				windows.getArea().append("ERROR\n");
			hide();
			waitFor();
		}
	};	
	
	static ActionListener bet = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			int temp;
			try
			{
				temp = Integer.parseInt(windows.getPrice().getText());
				writer.println("BET "+ windows.getPrice().getText());
				windows.getPrice().setText("");
			}
			catch(NumberFormatException e)
			{
				return;
			}
			String line = "";
			do
			{
				try
				{
					line = reader.readLine();
				}
				catch(IOException e)
				{
				}
			}
			while(!line.substring(0, 2).equals("OK") && !line.equals("ERROR"));
			
			if(line.substring(0, 2).equals("OK"))
			{
				try
				{
					String[] help = line.split(" ");
					windows.getLabel(6).setText(help[1]);
					windows.getLabel(8).setText(help[2]);
				}
				catch(NumberFormatException e)
				{
				}
			}
			else
				windows.getArea().append("ERROR\n");	
			hide();
			waitFor();		
		}
	};	
	
	static ActionListener raise = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			int temp;
			try
			{
				temp = Integer.parseInt(windows.getPrice().getText());
				writer.println("RAISE "+ windows.getPrice().getText());
				windows.getPrice().setText("");
			}
			catch(NumberFormatException e)
			{
				return;
			}
			
			String line = "";
			do
			{
				try
				{
					line = reader.readLine();
				}
				catch(IOException e)
				{
				}
			}
			while(!line.substring(0, 2).equals("OK") && !line.equals("ERROR"));
			
			if(line.substring(0, 2).equals("OK"))
			{
				try
				{
					String[] help = line.split(" ");
					windows.getLabel(6).setText(help[1]);
					windows.getLabel(8).setText(help[2]);
				}
				catch(NumberFormatException e)
				{
				}
			}
			else
				windows.getArea().append("ERROR\n");	
			hide();
			waitFor();
		}
	};	
	
	static ActionListener call = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			writer.println("CALL");
			String line = "";
			do
			{
				try
				{
					line = reader.readLine();
				}
				catch(IOException e)
				{
				}
			}
			while(!line.substring(0, 2).equals("OK") && !line.equals("ERROR"));
			
			if(line.substring(0, 2).equals("OK"))
			{
				try
				{
					String[] help = line.split(" ");
					windows.getLabel(6).setText(help[1]);
					windows.getLabel(8).setText(help[2]);
				}
				catch(NumberFormatException e)
				{
				}
			}
			else
				windows.getArea().append("ERROR\n");	
			hide();
			waitFor();
		}
	};
	
	static ActionListener fold = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			writer.println("FOLD");
			hide();
			waitFor();
		}
	};	
	
	static ActionListener allin = new ActionListener()
	{
		public void actionPerformed(ActionEvent evt)
		{
			writer.println("ALL-IN");
			String line = "";
			do
			{
				try
				{
					line = reader.readLine();
				}
				catch(IOException e)
				{
				}
			}
			while(!line.substring(0, 2).equals("OK") && !line.equals("ERROR"));
			
			if(line.substring(0, 2).equals("OK"))
			{
				try
				{
					String[] help = line.split(" ");
					windows.getLabel(6).setText(help[1]);
					windows.getLabel(8).setText(help[2]);
				}
				catch(NumberFormatException e)
				{
				}
			}
			else
				windows.getArea().append("ERROR\n");	
			hide();
			waitFor();	
		}
	};
}
